//
//  HomeDetailViewController.m
//  LOL
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 syx. All rights reserved.
//

#import "HomeDetailViewController.h"
#import "MBProgressHUD.h"
#define HomeDetail @"http://www.xiushuang.com/client/Portal/p_detail/lib/jq/id/422469/game/index.json"

@interface HomeDetailViewController ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView * webView;
@property (nonatomic,strong) MBProgressHUD *HUD;
@end

@implementation HomeDetailViewController

-(UIWebView *)webView{
    if (_webView == nil) {
        _webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
        self.HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getInformationFromURL];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    // Do any additional setup after loading the view.
}

// 获取数据
- (void)getInformationFromURL{
    NSString * string1 = @"http://www.xiushuang.com/client/Portal/p_detail/lib/jq/id/";
    NSString * string2 = @"/game/index.json";
    NSString * urlStr = [NSString stringWithFormat:@"%@%@%@",string1,self.urlStr,string2];
    NSURL * url = [NSURL URLWithString:urlStr];
    NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"解析详情页面出错");
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSString * string = dic[@"content"];
        
        dispatch_async(dispatch_get_main_queue(), ^{

            [self.webView loadHTMLString:string baseURL:nil];

        });
        
    }];
 
    [dataTask resume];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self.HUD performSelectorOnMainThread:@selector(hide:) withObject:nil waitUntilDone:YES];
}

- (void)leftButtonAction:(UIBarButtonItem *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
